<?php

namespace App\Services;

use App\Jobs\SendTemplateMsg;
use Illuminate\Support\Facades\Log;

class ExhibitionSendMsgService
{
//    传入weichatQueue的Collection
    public function sendMsg($weichat)
    {
//        dump($weichat);
        $miniProgram = \EasyWeChat::miniProgram(); // 小程序
        // 获取 access token 实例
        $accessToken = $miniProgram->access_token;
        $token = $accessToken->getToken(); // token 数组  token['access_token'] 字符串
        if (!is_array($token)){
        $token = $accessToken->getToken(true);
        }
        $access_token = $token['access_token'];
        $url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/uniform_send?access_token={$access_token}";
        foreach ($weichat as $key => $val) {
            if ($val->user->mini_openid) {
                dispatch(new SendTemplateMsg($val->user->mini_openid, $url));
            } elseif ($val->user->web_openid) {
                //没有小程序openid 有webopenid的话就通过webopenid发送
                dispatch(new SendTemplateMsg($val->user->web_openid, $url));
            }
            $val->forceDelete();
        }
    }
}
